#!/bin/sh

set -eo pipefail

export APP_ENV=${APP_ENV:-product}
export APP_NAME=${APP_NAME:-api}
export ENABLE_JOB=${ENABLE_JOB:-true}
export ENABLE_CRON=${ENABLE_CRON:-true}
export EXEC_CMD="${EXEC_CMD:-date}"
export ENABLE_XHPROF=${ENABLE_XHPROF:-false}
php -v
echo "检测当前环境:${APP_ENV}"
echo "更换yaf系统环境"
sed -i "s/product/${APP_ENV}/g" /usr/local/etc/php/conf.d/docker-php-ext-yaf.ini
if [[ `php -r "echo ini_get('xdebug.idekey');"` == "PHPSTORM" && ${APP_ENV} == "local" && ${APP_NAME} != "api" ]]
then
    echo "更换xdebug key"
    sed -i "s/PHPSTORM/${APP_NAME}/g" /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
fi

echo "执行自定义命令${EXEC_CMD}"
${EXEC_CMD}

if [[ ${ENABLE_JOB} == true ]]
then
    pm2 start /etc/pm2/apps.json
else
    echo "跳过JOB启动"
fi
if [[ ${ENABLE_CRON} == true ]]
then
    crond
else
    echo "跳过定时任务启动"
fi
if [[ ${ENABLE_XHPROF} == false ]]
then
    rm -fr /usr/local/etc/php/conf.d/docker-php-ext-xhprof.ini
else
    echo "启动xhprof"
fi
exec "$@"